package com.twang.stock.service.impl;

import com.twang.stock.entity.StockInfo;
import com.twang.stock.entity.StockInfoDto;
import com.twang.stock.mapper.StockInfoMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.email.SendmailService;
import com.twang.stock.utils.date.DateUtils;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 规则-01
 * 判断分时量规则
 */
@Slf4j
@Service
public class MinutesVolRuleService {

    @Autowired
    private SendmailService sendmailService;

    @Resource
    private StockInfoMapper stockInfoMapper;

    @Resource
    private RedisUtil redisUtil;

    /**
     * @param vol      分时
     * @param may1_vol 当日总量
     * @param code     代码
     * @param price    价格
     */
    public void excute(Integer vol, Integer may1_vol, String code, float price) {
//        halfVolRule(code, may1_vol);
//        todayTotalVol(code, may1_vol);
    }

    public void yiDongStock(String message,String codeName,String name,String rule){
        sendmailService.sendMail(message, codeName,name,rule);
    }

    /**
     * 半日成交量
     *
     * @param may1_vol
     */
    public void halfVolRule(String code, Integer may1_vol,Long userId) {
        StockInfo info = getCode(code, HolidayCopy.getRealToday());
        if (null == info || null == info.getVolMay1()) {
            return;
        }
//        //判断分时
        if (isTradeTime()) {
            //交易时间段
            if (DateUtils.JudgeTime("09:30:00", "11:30:00")) {
                String key = info.getCode() + "_" + info.getDate() + "may1_half_vol";
                Boolean flag = null == redisUtil.get(key) ? Boolean.FALSE : (Boolean) redisUtil.get(key);
                if (!flag) {
                    if (may1_vol > (info.getVolMay1() / 2)) {
                        StringBuffer str = new StringBuffer("[ ").append(info.getName()).append(" ] 上午交易呈现放量状态, 当前总量:").append(may1_vol)
                                .append("（手）  昨日成交量:").append(info.getVolMay1()).append("（手）");
                        sendmailService.sendMailWithEmail(str.toString(), info.getName(), info.getName(),userId);
                        redisUtil.set(key, Boolean.TRUE, 3600 * 4);
                    }
                }
            }
        }
    }

    /**
     * 当日呈现放量
     *
     * @param may1_vol
     */
    public void todayTotalVol(String code, Integer may1_vol,Long userId) {
        StockInfo info = getCode(code, HolidayCopy.getRealToday());
        if (null == info || null == info.getVolMay1()) {
            return;
        }
        //判断分时
        if (isTradeTime()) {
            if (may1_vol > (info.getVolMay1())) {
                String key = info.getCode() + "_" + info.getDate() + "may1_vol";
                Boolean flag = null == redisUtil.get(key) ? Boolean.FALSE : (Boolean) redisUtil.get(key);
                if (!flag) {
                    StringBuffer str = new StringBuffer("[ ").append(info.getName()).append(" ] 当日呈现放量状态, 当前总量:").append(may1_vol);
                    sendmailService.sendMailWithEmail(str.toString(), info.getName(), info.getName(),userId);
                    redisUtil.set(key, Boolean.TRUE, 3600 * 4);
                }
            }
        }
    }

    private Boolean isTradeTime() {
        if (DateUtils.JudgeTime("09:30:00", "11:30:00") || DateUtils.JudgeTime("13:00:00", "15:00:00")) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }


    /**
     * 获取当前时间上一个交易日数据
     * @param code
     * @param date
     * @return
     */
    public StockInfo getCode(String code, String date) {
        StockInfoDto param = new StockInfoDto();
        param.setCode(code);
        param.setDate(date);
        return stockInfoMapper.selctScondData(param);
    }

    public StockInfo getCodeToday(String code, String date) {
        StockInfoDto param = new StockInfoDto();
        param.setCode(code);
        param.setDate(date);
        return stockInfoMapper.selectOne(param);
    }

    /**
     * 获取最新的数据
     * @param cdoe
     * @return
     */
    public StockInfo getStockInfoByDateDESC(String cdoe) {
        Example example = new Example(StockInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", cdoe);
        example.orderBy("date").desc();
        List<StockInfo> stockInfoList=stockInfoMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(stockInfoList)){
            return null;
        }
        return stockInfoList.get(0);
    }
}
